﻿using System.Diagnostics;

namespace Ensemble_Windows
{
    /// <summary>
    /// 时间类
    /// </summary>
    public class Time
    {
        private Stopwatch _stopwatch { get; set; }
        private double _lastUpdate { get; set; }
        /// <summary>
        /// 时间标量
        /// </summary>
        public double ElapseTime
        {
            get
            {
                return _stopwatch.ElapsedMilliseconds * 0.001;
            }
        }

        /// <summary>
        /// Construct
        /// </summary>
        public Time()
        {
            _stopwatch = new Stopwatch();
        }

        /// <summary>
        /// 开始或继续时钟
        /// </summary>
        public void Start()
        {
            _stopwatch.Start();
            _lastUpdate = 0;
        }

        /// <summary>
        /// 停止时钟
        /// </summary>
        public void Stop()
        {
            _stopwatch.Stop();
        }

        /// <summary>
        /// 时钟更新
        /// </summary>
        /// <returns></returns>
        public double Update()
        {
            double now = ElapseTime;
            double updateTime = now - _lastUpdate;
            _lastUpdate = now;
            return updateTime;
        }
    }
}
